﻿#!/usr/local/bin/perl
$scriptHeader = "
===============================================================================
  This script is used to compile and install QT SDK from source:

  Usage:
      build-qt.sh src_path dst_path [static | dynamic]
    - src_path : QTSDK source path
    - dst_path : QTSDK install path
===============================================================================
";

use File::Path qw(make_path remove_tree);
use File::Spec;
use File::Copy;

#==============================================================================
sub dircopy
{
    my @src = $_[0];
    my @dst = $_[1];
    my $dir;

    until (scalar(@src)==0)
    {
        if(!opendir($dir,$src[0]))
        {
            return false;
        }

        mkdir($dst[0]);

        my @srcList = grep {!/^\.\.?$/} readdir $dir;

        for(my $i = 0; $i < @srcList; $i++)
        {
            if ( -f "$src[0]/$srcList[$i]" )
            {
                copy("$src[0]/$srcList[$i]", "$dst[0]/$srcList[$i]");
            }
            if ( -d "$src[0]/$srcList[$i]" )
            {
                push(@src,"$src[0]/$srcList[$i]");
                push(@dst,"$dst[0]/$srcList[$i]");
            }
        }
        closedir($dir);

        shift(@src);
        shift(@dst);
    }
}

#==============================================================================
print($scriptHeader);

if((@ARGV ne 2) and (@ARGV ne 3))
{
    exit(-1);
}

#==============================================================================
$srcPath = @ARGV[0];
$srcPath = File::Spec->rel2abs($srcPath);
$dstPath = @ARGV[1];
$dstPath = File::Spec->rel2abs($dstPath);

$binType = 'dynamic';
if(@ARGV eq 3)
{
    if((lc @ARGV[2] ne 'static') and (lc @ARGV[2] ne 'dynamic'))
    {
        exit(-1);
    }
    $binType = lc @ARGV[2];
}


#==============================================================================
$confArg = ' -prefix '.$dstPath.' -opensource -confirm-license ';
$confArg.= ' -nomake examples -opengl desktop -qt-xcb -fontconfig';
$confArg.= ' -silent';

if($binType eq 'static')
{
    $confArg.= ' -static'
}

#==============================================================================
$modList = ' qtbase             qtimageformats      qtdeclarative     
             qtxmlpatterns      qttools             qtdoc
             qtwebsockets       qttranslations      qtgraphicaleffects
             qtserialport       qtquickcontrols     qtmultimedia
             qtsvg              qtcanvas3d          qtquickcontrols2
             qtdatavis3d        qtcharts            qt3d
             qtx11extras ';

#==============================================================================
$ENV{'PATH'} .= ':'.$dstPath.'/bin';

$confCmd  = $srcPath.'/qtbase/configure '.$confArg;
$confCmd .= ' > conf-src.log 2>&1';

$makeCmd  = 'make -j4           > make-src.log 2>&1 ';
$instCmd  = 'make -j4 install   > inst-sdk.log 2>&1 ';
$makeDoc  = 'make docs          > make-doc.log 2>&1 ';
$instDoc  = 'make install_docs  > inst-doc.log 2>&1 ';

printf("Compile and install QT SDK (link mode: %s)\n", $binType);

@mods     = split(" ",$modList);
$modCount = @mods;
$current  = 0;

if(!(-e $srcPath.'/qtbase/.gitignore'))
{
    system("echo foo > ".$srcPath."/qtbase/.gitignore");
}

if( -e "./build-qt" )
{
    remove_tree("./build-qt");
}

mkdir("build-qt");
chdir("build-qt");

for($i = 0; $i < @mods; $i++)
{
    printf("(%02d of %02d) %s\n",($i + 1),$modCount,$mods[$i]);

    if(!(-e $srcPath."/".$mods[$i]))
    {
        printf("Module not exists.......");
        printf("ignored\n");
        next;
    }

    mkdir ($mods[$i]);
    chdir ($mods[$i]);

    printf("Configure source code...");
    if($mods[$i] ne "qtbase")
    {
        $confCmd  = $dstPath."/bin/qmake ";
        $confCmd .= $srcPath."/".$mods[$i]." > conf-src.log 2>&1";
    }

    if(system($confCmd) == 0)
    {
        print("succeed\n");
    }
    else
    {
        print("failed\n");
    }

    printf("Build module............");
    if(system($makeCmd) == 0)
    {
        print("succeed\n");
    }
    else
    {
        print("failed\n");
    }

    printf("Install module..........");
    if(system($instCmd) == 0)
    {
        print("succeed\n");
    }
    else
    {
        print("failed\n");
    }

    if(!(-e $srcPath."/".$mods[$i]."/examples"))
    {
        chdir ("..");
        next;
    }
    printf("Install examples........");

    if(dircopy($srcPath."/".$mods[$i]."/examples",
               $dstPath."/examples"))
    {
        print("succeed\n");
    }
    else
    {
        print("failed\n");
    }

    printf("\n");
    chdir ("..");
}

print("QT SDK installation completed\n");

if($binType eq 'static')
{
    exit(0);
}


print("Install QT SDK documents\n");
for($i = 0; $i < @mods; $i++)
{
    printf("(%02d of %02d) %s\n",($i + 1),$modCount,$mods[$i]);

    if(!(-e $srcPath."/".$mods[$i]))
    {
        printf("Module not exists.......");
        printf("ignored\n");
        next;
    }

    chdir ($mods[$i]);


    printf("Generate documents......");
    if(system($makeDoc) == 0)
    {
        print("succeed\n");
    }
    else
    {
        print("failed\n");
    }

    printf("Install documents.......");
    if(system($instDoc) == 0)
    {
        print("succeed\n");
    }
    else
    {
        print("failed\n");
    }

    printf("\n");
    chdir ("..");
}

print("Documents installation completed\n");
exit(0);



